package com.sheng.whyclass.work.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.work.event.AddAnswerEvent;

import de.greenrobot.event.EventBus;

public class AddAnswerFragment extends Fragment implements View.OnClickListener {
    private View mRootView = null;
    private TextView headTitle;
    private EditText topicInput;
    private EditText contentInput;
    private Button submitBtn;

    String topic;
    String content;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_add_answer, container, false);
        initView();
        return mRootView;
    }

    private void initView() {
        headTitle = (TextView) mRootView.findViewById(R.id.head_title);
        topicInput = (EditText) mRootView.findViewById(R.id.topic_input);
        contentInput = (EditText) mRootView.findViewById(R.id.content_input);
        submitBtn = (Button) mRootView.findViewById(R.id.submit_btn);
        submitBtn.setOnClickListener(this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public void onEventMainThread(AddAnswerEvent event) {


        int code = event.code;


        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            CustomToast.showToast(event.info);
            return;
        }

    }

    private void findStudentWork() {
        StringRequest request = HttpProcessManager.getInstance().addAnswer(Constant.HOST_ADD_ANSWER, topic, content);
        VolleyManager.getInstance(getActivity().getApplicationContext()).addToRequestQueue(request);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.submit_btn:
                topic = topicInput.getText().toString().trim();
                content = contentInput.getText().toString().trim();
                if(TextUtils.isEmpty(topic)&&TextUtils.isEmpty(content)){
                    CustomToast.showLongToast(getString(R.string.para_null));
                }else{
                    findStudentWork();
                }
        }

    }
}
